﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.BandedGrid;
using System.Collections;
using System.Linq;
using BusinessObjects;

namespace QuanLyTruong
{
    public partial class frmNV_DiemDanh : DevExpress.XtraEditors.XtraForm
    {
        public static int Month = 3;
        public static int YearID = 2010;
        public static int Year = Module.LayNamHocTheoMa(YearID, Month);

        public int DaysOfMonth = 0;
        public DateTime CurrentMonth;
        bool DaLuu = true;
        string mode = "";

        public frmNV_DiemDanh()
        {
            InitializeComponent();
        }

        private void frmNV_DiemDanh_Load(object sender, EventArgs e)
        {

            ConfigGridColumn();
            LoadData();
        }

        public void ConfigGridColumn()
        {
            // Number of day
            DaysOfMonth = (new DateTime(Year, Month + 1, 1).AddDays(-1)).Day;

            // config column
            GridBandCollection grdBandList = gv.Bands;

            for (int i = 1; i < DaysOfMonth + 1; i++)
            {
                string date = new DateTime(Year, Month, i).DayOfWeek.ToString();
                switch (date)
                {
                    case "Monday":
                        date = "Thứ Hai";
                        break;
                    case "Tuesday":
                        date = "Thứ Ba";
                        break;
                    case "Wednesday":
                        date = "Thứ Tư";
                        break;
                    case "Thursday":
                        date = "Thứ Năm";
                        break;
                    case "Friday":
                        date = "Thứ Sáu";
                        break;
                    case "Saturday":
                        date = "Thứ Bảy";
                        break;
                    case "Sunday":
                        date = "Chủ Nhật";
                        break;
                }

                grdBandList[i + 1].Caption = i + "/" + Month + "\n" + date;
                
            }

            if (DaysOfMonth == 28)
            {
                grdBandList[DaysOfMonth + 1 + 1].Visible = false;
                grdBandList[DaysOfMonth + 2 + 1].Visible = false;
                grdBandList[DaysOfMonth + 3 + 1].Visible = false;
            }
            if (DaysOfMonth == 29)
            {
                grdBandList[DaysOfMonth + 1 + 1].Visible = false;
                grdBandList[DaysOfMonth + 2 + 1].Visible = false;
            }
            if (DaysOfMonth == 30)
            {
                grdBandList[DaysOfMonth + 1 + 1].Visible = false;
            }
        }

        private void LoadData()
        {
            IList<NhanSu> dsNhanSu = new NhanSu().NhanSuAll();
            IList<NhanSu_HoatDong> dsNhanSuHoatDong = new NhanSu_HoatDong().GetNhanSu_HoatDong(Month, YearID);
            
            // Build datatable for grid
            DataTable tb = new DataTable();
            DataRow dr = tb.NewRow();
            tb.Columns.Add("STT");
            tb.Columns.Add("MaNhanSu");
            tb.Columns.Add("TenNhanSu");
            for (int a = 1; a < DaysOfMonth + 1; a++)
            {
                tb.Columns.Add(a.ToString() + "_1/2", typeof(Boolean));
                tb.Columns.Add(a.ToString() + "_1", typeof(Boolean));
            }

            // Bind data
            // If Exist data in NhanSu_HoatDong
            if (dsNhanSuHoatDong.Count > 0)
            {
                mode = "Update";
                for (int i = 0; i < dsNhanSuHoatDong.Count; i++)
                {
                    dr = tb.NewRow();
                    dr["STT"] = i + 1;
                    dr["MaNhanSu"] = dsNhanSuHoatDong[i].MaNhanSu;
                    var nhansu = from ns in dsNhanSu
                                 where ns.MaNhanSu == dsNhanSuHoatDong[i].MaNhanSu
                                 select ns;
                    foreach (var n in nhansu)
                    {
                        dr["TenNhanSu"] = n.Ho + " " + n.Ten;
                    }
                    for (int b = 1; b < DaysOfMonth + 1; b++)
                    {
                        dr[b.ToString() + "_1/2"] = Convert.ToBoolean(false);
                        dr[b.ToString() + "_1"] = Convert.ToBoolean(false);
                    }
                    tb.Rows.Add(dr);

                }
            }
            else
            {
                mode = "Insert";
                // Bind empty data
                for (int i = 0; i < dsNhanSu.Count; i++)
                {
                    dr = tb.NewRow();
                    dr["STT"] = i + 1;
                    dr["MaNhanSu"] = dsNhanSu[i].MaNhanSu;
                    dr["TenNhanSu"] = dsNhanSu[i].Ho + " " + dsNhanSu[i].Ten;
                    for (int b = 1; b < DaysOfMonth + 1; b++)
                    {
                        dr[b.ToString() + "_1/2"] = Convert.ToBoolean(false);
                        dr[b.ToString() + "_1"] = Convert.ToBoolean(false);
                    }
                    tb.Rows.Add(dr);
                }
            }

            grdNhanVien.DataSource = tb;

            // Set data for grid if exist
            for (int i = 0; i < dsNhanSuHoatDong.Count; i++)
            {
                string[] nghi1ngay = dsNhanSuHoatDong[i].NghiMotNgay.Split(',');
                string[] nghi12ngay = dsNhanSuHoatDong[i].NghiNuaNgay.Split(',');

                for (int k = 0; k < nghi1ngay.Length; k++)
                {
                    gv.SetRowCellValue(i, nghi1ngay[k] + "_1", true);
                }
                for (int l = 0; l < nghi12ngay.Length; l++)
                {
                    gv.SetRowCellValue(i, nghi12ngay[l] + "_1/2", true);
                }

            }
            
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (!DaLuu)
            {
                if (XtraMessageBox.Show("Bạn có muốn lưu lại những thay đổi không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
                {
                    ThucHienLuu();
                }
            }
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            ThucHienLuu();
            XtraMessageBox.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void ThucHienLuu()
        {
            try
            {

                for (int i = 0; i < gv.RowCount; i++)
                {
                    NhanSu_HoatDong nhansu = new NhanSu_HoatDong();
                    nhansu.MaNamHoc = YearID;
                    nhansu.Thang = Month;
                    nhansu.MaNhanSu = Convert.ToInt32(gv.GetRowCellValue(i, "MaNhanSu").ToString());

                    string nghi1ngay = "";
                    string nghi12ngay = "";
                    for (int j = 1; j < DaysOfMonth; j++)
                    {
                        if (Convert.ToBoolean(gv.GetRowCellValue(i, j + "_1")))
                        {
                            nghi1ngay += j + ",";
                        }
                        if (Convert.ToBoolean(gv.GetRowCellValue(i, j + "_1/2")))
                        {
                            nghi12ngay += j + ",";
                        }
                    }

                    nhansu.NghiMotNgay = nghi1ngay.TrimEnd(',');
                    nhansu.NghiNuaNgay = nghi12ngay.TrimEnd(',');

                    if (mode == "Update")
                    {
                        nhansu.Update();
                    }
                    else if (mode == "Insert")
                    {
                        nhansu.Create();
                    }
                }
                DaLuu = true;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("Có lỗi trong quá trình lưu \n" + ex.InnerException, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            
        }

        private void gv_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            DaLuu = false;
        }


    }
}